package com.atguigu.realtime.gmalllogger.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author lzc
 * @Date 2022/3/14 15:15
 */
@RestController
@Slf4j
public class LoggerController {
    
    @RequestMapping("/applog")
    public String logger(@RequestParam("param") String logStr) {
        
        // 1. 把数据写入到磁盘
        saveToDisk(logStr);
        
        // 2. 把日志数据写入到Kafka中
        writeToKafka(logStr);
        
        
        return "ok";
        
    }
    
    // 自动注入模板对象
    @Autowired
    KafkaTemplate<String, String> kafka;
    
    private void writeToKafka(String logStr) {
        // ? 者
        kafka.send("ods_log", logStr);
    }
    
    private void saveToDisk(String logStr) {
        //
        log.info(logStr);
    }
}
